import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Leet368 {
    public static void main(String[] args) {
        Leet368 l = new Leet368();
        for (Integer integer : l.largestDivisibleSubset(new int[]{1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824})) {
            System.out.println(integer);
        }
    }
    static int max;
    static List<Integer> maxL;
    public List<Integer> largestDivisibleSubset(int[] nums) {
        max = 0;
        maxL = new ArrayList<>();
        if (nums.length==1){
            maxL = new ArrayList<>();
            maxL.add(nums[0]);
            return maxL;
        }
        Arrays.sort(nums);
        for (int i = 0; i < nums.length-1; i++) {
            List<Integer> list = new ArrayList<>();
            list.add(nums[i]);
            dfs(nums,i+1,nums[i],list);
        }
        return maxL;
    }
    void dfs(int[] nums,int at,int last,List<Integer> list){
        if (at == nums.length){
            if (list.size()>max){
                max = list.size();
                maxL = new ArrayList<>(list);
            }
            return;
        }
        if (maxL.size()==nums.length)
            return;
        for (int i = at; i < nums.length; i++) {
            if (nums[i]<last*2)
                continue;
            if (nums[i]%list.get(list.size()-1)!=0)
                continue;
            list.add(nums[i]);
            dfs(nums,i+1,nums[i],list);
            list.remove(list.size()-1);
        }
        if (list.size()>max){
            max = list.size();
            maxL = new ArrayList<>(list);
        }
    }
}
